package com.GamersCom.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.GamersCom.util.GlobalCONST;

/**
 * Servlet implementation class AdminDispatcher
 */
public class AdminDispatcher extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		// retrieve session information
		String username = (String) session.getAttribute("username");
		Integer access = (Integer) session.getAttribute("accesslevel");

		String nextView; // the nextView to be called

		// validate user status
		// if user not logged in, direct to the login page; if it's regular
		// user, display error message
		if (username == null) // if user is not logged in
		{
			nextView = "/login.jsp";
			request.getRequestDispatcher(nextView).forward(request, response);
		
		}
		else
		{
			// authenticate the user by accessLevel
			if (GlobalCONST.ADMINISTRATOR != access)
			{
				String error = "You are not allowed to view this page!";
				request.setAttribute("error", error);
				nextView = "result.jsp";
				request.getRequestDispatcher(nextView).forward(request, response);
			}
			else
			{
				// administrator operation area
				String url = request.getPathInfo();

				// url like: /admin, show 404 error
				if (url == null)
				{
					System.out.println("404 page not found!");
//					request.getRequestDispatcher(nextView).forward(request, response);
				}
				else
				{
					// parse the url
					String[] path = url.split("/");

					// url like /admin/ , show 404 error
					if (path.length == 0)
					{
						System.out.println("404 page not found!");
//						request.getRequestDispatcher(nextView).forward(request, response);
					}
					else
					{
				System.out.println("path[1] : "+path[1]);		
						// dispatch the url to the corresponding servlet
						// URL: /admin/users
						if ( path[1].equals("users"))
						{
							this.getServletContext()
							.getNamedDispatcher("AdminUsers")
							.forward(request, response);
						}
						
						// URL: /admin/platforms
						else if (path[1].equals("platforms"))
						{
							System.out.println("/admin/platform");
							this.getServletContext()
							.getNamedDispatcher("AdminPlatforms")
							.forward(request, response);
						}
						
						// URL: /admin/genres
						else if (path[1].equals("genres"))
						{
							this.getServletContext()
							.getNamedDispatcher("AdminGenre")
							.forward(request, response);
						}
						
						// URL: /admin/games
						else if (path[1].equals("games"))
						{
							this.getServletContext()
									.getNamedDispatcher("AdminGames")
									.forward(request, response);
						}												
						
						// URL: /admin/game
						else if ( path[1].equals("game") ) 
						{
							
							if ( path.length <= 2 )
							{
								System.out.println("404 page not found!");
							}
							else
							{
								//validate the game id 
								if ( path[2].matches("[0-9]++") )
								{
									request.setAttribute("gameId", path[2]);
									this.getServletContext()
									.getNamedDispatcher("AdminGameDetail")
									.forward(request, response);
								}
								else 
								{
									System.out.println("404 page not found!");
								}								
							}
							
						}
													
						else
						{
							System.out.println("who am I??");
						}
					}
				}

			}
			
			
		}

	}
}
